﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PDAMain.DAL;
using PDAMain.Forms.ReadingAmmeter;
using System.IO;
using System.Runtime.InteropServices;


namespace PDAMain.Forms.ReadingAmmeter
{
    
    public partial class AmmeterRecordDetail : Form
    {
        private int NowN;
        private int PreN;
        private bool isTakePictrue;
        private string MPRId;
        public AmmeterRecordDetail()
        {
            InitializeComponent();
            BindDate();
        }

        private void menuComfirm_Click(object sender, EventArgs e)
        {
            //先判断数据类型
            string result = VerifyNumberData();
                if (result == "0") 
            {
                MessageBox.Show("请先输入本月读数！");
                return;
            }
            else if (result == "1")
            {
                MessageBox.Show("录入的本月读数不是有效地数据类型！");
                return;
            }
            else if (result == "2")
            {
                DialogResult dr;
                dr = MessageBox.Show("当前读数小于上月读数,是否继续？", "MessageBox", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                if (dr == DialogResult.Cancel)
                {
                    txtnownumber.Text = "";
                    return;
                }
            }
            if (!isTakePictrue)
            {
                MessageBox.Show("请先对电表读数进行拍照！");
                return;
            }

            saveData();
          /*  //数据类型正确且拍照则保存数据
           if (blVND)
           {
               if (blPicture)
               {
                   UpdateData();
               }
               else
               {
                   MessageBox.Show("请先拍照！");
               }
           }*/
        }

         private void saveData()
        {
            string degree = txtnownumber.Text.ToString().Trim();
            string completedt = DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss").Trim();
            /*if (DataHelper.ReadingAmmeterDao.deal(DataHelper.ReadingAmmeterDao.MprId, degree, completedt, DataHelper.UserDao.getLoginedUserAccount()))
            {
                MessageBox.Show("成功同步数据至远程数据库");
            }
            else
            {*/
                if (DataHelper.ReadingAmmeterDao.insertLacolTable(DataHelper.ReadingAmmeterDao.MprId, degree, completedt, DataHelper.UserDao.getLoginedUserAccount()))
                {
                    MessageBox.Show("保存数据成功！");
                }
                else
                {
                    MessageBox.Show("保存数据失败！");
                }
            //}
            backToList();
        }

         private void backToList()
         {
             AmmeterReadingList list = new AmmeterReadingList();
             list.Show();
             this.Close();
         }

        private void menuBack_Click(object sender, EventArgs e)
        {
            backToList();
        }

        private string VerifyNumberData()
        {
            string Flag = "-1";
            if (txtnownumber.Text.ToString().Trim() == "" || txtnownumber.Text.ToString().Trim() == null)
            {
                Flag = "0";
                return Flag;
            }

            try
            {
                NowN = Convert.ToInt32(txtnownumber.Text.ToString().Trim());
            }
            catch
            {
                txtnownumber.Text = "";
                Flag = "1";
                return Flag;
            }

            if (lblpredegree.Text.ToString().Trim() != null || lblpredegree.Text.ToString().Trim() != "")
            {
                PreN = Convert.ToInt32(lblpredegree.Text.ToString().Trim());
            }
            else
            {
                PreN = 0;
            }

            if (PreN > NowN)
            {
                Flag = "2";

                /*DialogResult dr;
                dr = MessageBox.Show("当前读数小于上月读数,是否继续？", "MessageBox", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                if (dr == DialogResult.OK)
                {
                    Flag = true;
                }
                else
                {
                    txtnownumber.Text = "";
                    Flag = false;
                }*/
            }
            return Flag;
        }
        
        private void BindDate()
        {
            /*string ENum = Identity.EM_METER_ID;
            string strsql = " SELECT MPRId,EM_METER_ID,READMONTH,PRE_DEGREE,READDEGREE,DISTRICT_ID,"
                            + "BUILDING_ID,APARMENT_ID FROM ELECTRICITYMETER WHERE EM_METER_ID='" + ENum + "'";
            DataSet ds = PublicMethods.SelectData(strsql);*/
            DataSet ds = DataHelper.ReadingAmmeterDao.getDetail(DataHelper.ReadingAmmeterDao.MprId);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                 MPRId = ds.Tables[0].Rows[0]["MPRId"].ToString().Trim();
                lblEamterID.Text = ds.Tables[0].Rows[0]["EM_METER_ID"].ToString().Trim();
                lblpredegree.Text = ds.Tables[0].Rows[0]["PRE_DEGREE"].ToString().Trim();
                lbldistrict.Text = ds.Tables[0].Rows[0]["DISTRICT_ID"].ToString().Trim();
                lblbuilding.Text = ds.Tables[0].Rows[0]["BUILDING_ID"].ToString().Trim();
                lblapatment.Text = ds.Tables[0].Rows[0]["APARMENT_ID"].ToString().Trim();
                lblBarcode.Text = ds.Tables[0].Rows[0]["BARCODE"].ToString().Trim();
            }
        }

        /*
         public void UpdateData()
         {
             string completedt = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss").Trim();
             string sql = "UPDATE ELECTRICITYMETER SET READDEGREE='" + NowN + "',READINGTIME='" + completedt + "' WHERE EM_METER_ID='" + lblEamterID.Text + "'";
             bool Flag= PublicMethods.InsertUpdateCreateDropData(sql);
             if (Flag)
             {

                 //保存成功则更新数据
                 pdaWebReference.PdaService pda = PublicMethods.getobject();
               
                 string degree=txtnownumber.Text.ToString();

                 try
                 {
                     bool blFlag = pda.updateAmmeterReadingWorkOrder(MPRId, degree, completedt, Identity.dutyPerson);
                     string delsql = " DELETE FROM ELECTRICITYMETER WHERE EM_METER_ID='" + lblEamterID.Text + "'";
                     bool delFlag = PublicMethods.InsertUpdateCreateDropData(delsql);
                     AmmeterReadingList ARL = new AmmeterReadingList();
                     ARL.Show();
                     this.Close();
                 }
                 catch
                 {
                     MessageBox.Show("更新失败！");
                 }
             }
             else
             {
                 MessageBox.Show("保存失败！");
             }
         }
         */
        private void btnPicture_Click(object sender, EventArgs e)
        {
            try
            {
                TakePicture TakeP = new TakePicture();
                string EMeterID = lblEamterID.Text.ToString();
                string folderName = "Ammeter";
                //string strFileName = EMeterID;
                string strFileName = DataHelper.ReadingAmmeterDao.MprId + "_" + DateTime.Now.ToString("yyyyMMddHHmmss"); ;
                Bitmap bp = TakeP.Show(folderName, strFileName);
                if (bp != null)
                {
                    isTakePictrue = true;
                    pictureL.Image = bp;
                }
                TakeP.dispose();
            }
            catch
            {
                MessageBox.Show("点击拍照失败，请重新操作");
            }
        }

        private void lblapatment_ParentChanged(object sender, EventArgs e)
        {

        }
               
    }
}